package com.xm.security.exception;

import com.xm.security.config.security.SecurityConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yousj
 * @since 2021/8/27
 */
@Component
@RequiredArgsConstructor
@ConditionalOnClass(SecurityConfig.class)
public class ExceptionFilter extends OncePerRequestFilter {

    private final ExceptionAdviceHandler exceptionAdviceHandler;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            exceptionAdviceHandler.handler(e, response);
        }
    }
}
